<?php

require_once('./config/config.php');
require_once('./config/error_handler.php');

// Profile class that contains server-side profiel
class WerkgeverProfielDB {

    // database handler
    private $mMysqli;

    // constructor opens database connection
    function __construct() {
        // connect to the database
        $this->mMysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE);
    }
    
    // destructor closes database connection
    public function __destruct() {
        $this->mMysqli->close();
    }
    
    //The postMessages method inserts a profile into the database
    public function postMessage($instellingNaam, $adres, $postcode, $woonplaats, $provincie, $telefoon, $login, $wachtwoord) {

        // escape the variable data for safely adding them to the database
        $instellingNaam = $this->mMysqli->real_escape_string($instellingNaam);
        $adres = $this->mMysqli->real_escape_string($adres);
        $postcode = $this->mMysqli->real_escape_string($postcode);
        $woonplaats = $this->mMysqli->real_escape_string($woonplaats);
        $provincie = $this->mMysqli->real_escape_string($provincie);
        $telefoon = $this->mMysqli->real_escape_string($telefoon);
        $login = $this->mMysqli->real_escape_string($login);
        $wachtwoord = $this->mMysqli->real_escape_string($wachtwoord);

        
        
        // check for companyname
        $querycheck = "SELECT * FROM instelling WHERE instelling_naam ='$instellingNaam'";
        $resultcheck = $this->mMysqli->query($querycheck);
        if ( mysqli_num_rows($resultcheck) == null ) {
//          
            $query = 'INSERT INTO `instelling`(`instelling_naam`, `adres`, `postcode`, 
                `woonplaats`, `provincie`, `telnr`, `login`, `wachtwoord`) ' .
            'VALUES ("' . $instellingNaam . '","' . $adres . '","' . $postcode . '",
                "' . $woonplaats . '","' . $provincie . '","' . $telefoon . '",
                "' . $login . '","' . $wachtwoord . '")';
            
            // execute the SQL query
            $result = $this->mMysqli->query($query);
        }
    }
}

?>
